home *** CD-ROM | disk | FTP | other *** search
- /* >C.Tree */
-
- /******************************************************************************
- TREE (c) D.J.Scott 1988
- *******************************************************************************
- Program to print ADFS directory / file list or search for files.
-
- Command line parameters are <directory> <filename> <options>
- Syntax: *Tree [<directoryname>] [<wildcard file name>] [options]
- If no parameters are given then the help text is displayed
- Default directoryname is ':4.$' default wild card filename is '*'
- Options are:
- -c current directory only, default includes subdirectories
- -d directory names only, default includes file names
- *****************************************************************************/
-
- static char *progid = "TREE 1.10 - 27/01/89";
-
- /*************************** INCLUDE DIRECTIVES ******************************/
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <signal.h>
- #include <arthur.h>
-
- /*************************** DEFINE DIRECTIVES *******************************/
-
- #define ADFS_FreeSpace 0x40243
- #define FALSE 0
- #define TRUE 1
- #define OPTTOT 5
-
- /*************************** GLOBAL VARIABLES ********************************/
-
- int dircnt = 0; /* directory count */
- int filcnt = 0; /* file count */
- int indent = 0; /* file list indentation */
- int sdircnt = 0; /* subdirectory count */
-
- int copt = FALSE; /* current directory only option */
- int dopt = FALSE; /* directory list only option */
- int lopt = FALSE; /* list option */
- int popt = FALSE; /* print option */
- int sopt = FALSE; /* search option */
-
- char sname[12]; /* source file name */
- char sdir[120]; /* source directory name */
-
- static char *driv = ":4."; /* default drive */
- static char *root = "$"; /* root directory name */
- static char *wild = "*"; /* wild card file name */
-
- static char *optlist[OPTTOT] = /* option list */
- {
- "-c", /* current directory only */
- "-d", /* directory names only */
- "-l", /* list in single column */
- "-p", /* print output */
- "-s", /* search option */
- };
-
- /*************************** FUNCTION DEFINITIONS ****************************/
-
- void chkerr(struct error *);
- void command(int, char *);
- void countfile(char *);
- void escape(int);
- void freespc(char *);
- void helptext(void);
- void readdir(char *, char *);
- void setoption(char *);
-
- /******************************************************************************
- main main program
- argc number of command line parameters
- argv array of parameter string pointers
- ******************************************************************************/
- int main(argc, argv)
- int argc; /* count of command line arguments */
- char *argv[]; /* pointers to argument strings */
- {
- static char *optchar = "-"; /* option command introducer */
- static char *drvchar = ":"; /* drive introducer */
- char *sptr; /* string pointer */
- int i = 1;
-
- signal(SIGINT, escape);
-
- /* process command line */
- if (argc == 1)
- {
- helptext(); /* null parameters, print help text */
- exit(0);
- return(0);
- }
- while (argc > i)
- {
- if (strncmp(optchar, argv[i], 1) == 0)
- setoption(argv[i]);
- else if (i == 1)
- {
- if (strncmp(drvchar, argv[i], 1) != 0)
- {
- strcpy(sdir, driv);
- strcat(sdir, argv[i]);
- }
- else
- strcpy(sdir, argv[1]);
- }
- else if (i == 2)
- strcpy(sname, argv[2]);
- else if (i > 2)
- {
- fprintf(stderr, "\n** Unknown command parameter\n\a");
- helptext();
- exit(1);
- }
- i++;
- }
-
- /* set default directory and wildcard file names */
- if (strlen(sdir) == 0)
- {
- strcpy(sdir, driv);
- strcat(sdir, root);
- }
- if (strlen(sname) == 0)
- strcpy(sname, wild);
-
- printf("\n");
- if (popt == TRUE)
- vdu(2);
- readdir(sdir, sname);
- printf("\n%d files and %d subdirectories with name %s in %d directories\n", filcnt, sdircnt, sname, dircnt);
- sptr = strtok(sdir, ".");
- freespc(sptr);
- if (popt == TRUE)
- vdu(3);
- exit(0);
- return(0);
- }
-
- /******************************************************************************
- chkerr checks for Operating System error and prints details if found
- ******************************************************************************/
- void chkerr(e)
- struct error *e;
- {
- if (e != 0)
- {
- fprintf(stderr, "\n** %s Error &%x\n\a", e->errmess, e->errnum & 0xFFF);
- exit(1); /* terminate as fatal error */
- }
- return;
- }
-
- /******************************************************************************
- countfile procedure to count the number and size of files in path
- path path name string pointer
- ******************************************************************************/
- void countfile(path)
- char *path;
- {
- int i;
- reg_set cnt; /* register set for count objects call */
-
- for (i = 0; i < 10; i++)
- cnt.r[i] = 0; /* clear register set */
- cnt.r[0] = 28;
- cnt.r[1] = (int)path; /* path name to be counted */
- cnt.r[3] = 256;
- if (popt == TRUE)
- vdu(3);
- vdu(21);
- cnt = swi(OS_FSControl, &cnt); /* perform count */
- vdu(6);
- if (popt == TRUE)
- vdu(2);
- printf("%6d files: %9d bytes\n", cnt.r[3], cnt.r[2]);
- return;
- }
-
- /******************************************************************************
- escape
- ******************************************************************************/
- void escape(signo)
- int signo;
- {
- signal(signo, SIG_IGN);
- printf("\nEscape\n");
- exit(1);
- return;
- }
-
- /******************************************************************************
- freespc obtain and print the amount of free space left on the disc
- disc string giving the disc identity
- ******************************************************************************/
- void freespc(disc)
- char *disc;
- {
- reg_set fs;
-
- fs.r[0] = (int)disc;
- chkerr(swix(ADFS_FreeSpace, &fs));
- printf("%d bytes free on disc %s, largest space %d bytes\n", fs.r[0], disc, fs.r[1]);
- return;
- }
-
- /******************************************************************************
- helptext outputs help text
- ******************************************************************************/
- void helptext()
- {
- printf("%30s (c) 1988 D.J.Scott\n", progid);
- printf("List directory / file structure or search for files\n\n");
- printf("Syntax: *TREE [<:drive.directory>] [<wildcard file name>] [options]\n");
- printf(" If no parameters are given then the help text is displayed\n");
- printf(" Default drive and directory is :4.$, wild card file name is *\n");
- printf("Options are:\n");
- printf(" -c current directory only, default includes subdirectories\n");
- printf(" -d directory names only, default includes file names\n");
- printf(" -l list files in single column with indentation\n");
- printf(" -p send all screen information to printer\n");
- printf(" -s search for all files matching the wildcard file name\n");
- return;
- }
-
- /******************************************************************************
- readdir reads and displays directory contents
- dirnam directory path name string pointer
- filnam (wild card) file name string pointer
- ******************************************************************************/
- void readdir(dirnam, filnam)
- char *dirnam;
- char *filnam;
- {
- char pathnam[250]; /* path name string */
- static char *sep = "."; /* separator string */
-
- struct dirlist /* directory list record structure */
- {
- int loadaddr; /* load address */
- int execaddr; /* execution address */
- int filelength; /* file length */
- int fileattrib; /* file attributes */
- int objtype; /* object type */
- char objname[12]; /* object name */
- } dl;
-
- osgbpb_block db;
-
- /* read and print each directory name and details */
- dircnt++;
- indent += 2; /* increase file list indent */
- if (sopt == FALSE)
- {
- /* print directory details */
- printf("%-50s", dirnam);
- countfile(dirnam);
- }
-
- /* set up data block for osgbpb to read directory contents */
- db.action = 10; /* read directory entries */
- db.file_handle = (int)dirnam; /* pointer to directory name */
- db.data_addr = &dl; /* address of data */
- db.number = 1; /* number of object names to read */
- db.buf_len = sizeof (dl); /* buffer length */
- db.wild_fld = filnam; /* wildcard name to match */
- db.seq_point = 0; /* offset of first item to read from directory */
- do
- {
- chkerr(osgbpb(&db)); /* read directory */
- if (db.number != 0 && db.seq_point >= 0)
- {
- if (dl.objtype == 2) /* directory */
- {
- sdircnt++;
- if (copt == FALSE)
- {
- if (lopt == TRUE)
- {
- strcpy(pathnam, dirnam); /* form pathname */
- strcat(pathnam, sep);
- strcat(pathnam, dl.objname);
- readdir(pathnam, filnam); /* read directory contents */
- }
- if (sopt == TRUE)
- printf("%s.%s\n", dirnam, dl.objname); /* print directory name */
- }
- }
- else
- {
- if (dl.objtype == 1) /* file */
- {
- filcnt++;
- if (dopt == FALSE)
- {
- if (sopt == TRUE)
- printf(" %s.%s\n", dirnam, dl.objname); /* print full file name */
- else if (lopt == TRUE)
- printf("%*s%s\n", indent, "", dl.objname); /* print indented file name */
- else
- printf(" %-14s", dl.objname); /* print short file name */
- }
- }
- }
- }
- }
- while (db.seq_point >= 0);
- if (pos () > 1)
- printf("\n");
-
- /* set up data block for osgbpb to reread directory contents */
- db.number = 1; /* number of object names to read */
- db.wild_fld = wild; /* wildcard name to match */
- db.seq_point = 0; /* offset of first item read from directory */
- do
- {
- chkerr(osgbpb(&db)); /* read directory */
- if (db.number != 0 && db.seq_point >= 0)
- {
- if (dl.objtype == 2) /* directory */
- {
- if (copt == FALSE && lopt == FALSE)
- {
- /* print contents of directory */
- strcpy(pathnam, dirnam); /* form pathname */
- strcat(pathnam, sep);
- strcat(pathnam, dl.objname);
- readdir(pathnam, filnam); /* read directory contents */
- }
- }
- }
- }
- while (db.seq_point >= 0);
- indent -= 2;
- return;
- }
-
- /******************************************************************************
- setoption sets the option flag determined by option string
- opt pointer to option parameter string
- ******************************************************************************/
- void setoption(opt)
- char *opt;
- {
- int found = FALSE;
- int n;
-
- for (n = 0; n < OPTTOT; n++)
- {
- if (strncmp(opt, optlist[n], 2) == 0)
- {
- found = TRUE;
- switch(n)
- {
- case 0 :
- copt = TRUE;
- break;
- case 1 :
- dopt = TRUE;
- break;
- case 2 :
- lopt = TRUE;
- break;
- case 3 :
- popt = TRUE;
- break;
- case 4 :
- sopt = TRUE;
- break;
- }
- }
- }
- if (found == FALSE)
- {
- fprintf(stderr, "\n** Unknown option parameter\n\a");
- helptext();
- exit(1);
- }
- return;
- }
-
- /* end of file */
-